package com.android.clinic.app.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.clinic.app.R;

/**
 * Created by cheng on 2018/1/30.
 */
public class AccordionTextView extends LinearLayout {

    private Context mContext;

    private ImageView mImageIcon;   // 左边icon图标
    private TextView mTextName;     // 左边文本
    private TextView mTextValue;    // 右边文本
    private ImageView mImageArrow;  // 右边箭头
    private ImageView mImagePoint;  // 右边指示点

    private View mDivider;

    public AccordionTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater.from(context)
                .inflate(R.layout.view_accordion_textview, this);

        mImageIcon = (ImageView) findViewById(R.id.image_icon);
        mTextName = (TextView) findViewById(R.id.text_name);
        mTextValue = (TextView) findViewById(R.id.text_value);
        mImageArrow = (ImageView) findViewById(R.id.image_arrow);
        mImagePoint = (ImageView) findViewById(R.id.image_point);
        mDivider = findViewById(R.id.divider_line);

        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.AccordionTextView);

        initDrawableLeft(ta);
        initTextName(ta);
        initTextValue(ta);
        initDrawableRight(ta);
        initPoint(ta);

        // 默认显示分割线
        boolean isShowDivider = ta.getBoolean(
                R.styleable.AccordionTextView_accShowDivider, true);
        mDivider.setVisibility(isShowDivider ? View.VISIBLE : View.GONE);

        ta.recycle();
    }

    /**
     * 初始化icon
     * @param ta
     */
    private void initDrawableLeft(TypedArray ta) {
        boolean isShowIcon = ta.getBoolean(
                R.styleable.AccordionTextView_accShowIcon, true);
        if (isShowIcon) {
            int icon = ta.getResourceId(R.styleable.AccordionTextView_accDrawableLeft,
                    0);
            int iconLeft = (int) ta.getDimension(
                    R.styleable.AccordionTextView_accIconMarginLeft, dp2px(30f));
            MarginLayoutParams lp = (MarginLayoutParams) mImageIcon
                    .getLayoutParams();
            lp.leftMargin = iconLeft;
            mImageIcon.setLayoutParams(lp);
            mImageIcon.setImageResource(icon);
        } else {
            mImageIcon.setVisibility(View.GONE);
        }
    }

    /**
     * 初始化左边文本
     * @param ta
     */
    private void initTextName(TypedArray ta) {
        String key = ta.getString(R.styleable.AccordionTextView_accName);
        mTextName.setText(key);

        int keyTextSize = (int) ta.getDimension(
                R.styleable.AccordionTextView_accTextSize, 15f);
        mTextName.setTextSize(TypedValue.COMPLEX_UNIT_DIP, keyTextSize);

        if (mImageIcon.getVisibility() == View.GONE || mImageArrow.getVisibility() == View.VISIBLE) {
            int keyLeftMargin = (int) ta.getDimension(
                    R.styleable.AccordionTextView_accMarginLeft, dp2px(20f));
            MarginLayoutParams lp = (MarginLayoutParams) mTextName
                    .getLayoutParams();
            lp.leftMargin = keyLeftMargin;
            mTextName.setLayoutParams(lp);
        }
    }

    /**
     * 初始化右边文本
     * @param ta
     */
    private void initTextValue(TypedArray ta) {
        String value = ta.getString(R.styleable.AccordionTextView_accValue);
        mTextValue.setText(value);
        // 字体大小
        int valueTextSize = (int) ta.getDimension(
                R.styleable.AccordionTextView_accTextSize, 15f);
        mTextValue.setTextSize(TypedValue.COMPLEX_UNIT_DIP, valueTextSize);
        // 文字颜色
        int color = ta.getColor(R.styleable.AccordionTextView_accValueColor,
                ContextCompat.getColor(mContext, R.color.clinic_black));
        mTextValue.setTextColor(color);
        int valueMarginRight = (int) ta.getDimension(
                R.styleable.AccordionTextView_accValueMarginRight, dp2px(20f));
        MarginLayoutParams lv = (MarginLayoutParams) mTextValue
                .getLayoutParams();
        lv.rightMargin = valueMarginRight;
        mTextValue.setLayoutParams(lv);
    }

    /**
     * 初始化右边箭头
     * @param ta
     */
    private void initDrawableRight(TypedArray ta) {
        boolean isShowArrow = ta.getBoolean(
                R.styleable.AccordionTextView_accShowArrow, true);
        if (!isShowArrow) {
            mImageArrow.setVisibility(View.GONE);

        } else {
            int arrowRight = (int) ta.getDimension(
                    R.styleable.AccordionTextView_accArrowMarginRight, dp2px(20f));
            MarginLayoutParams lp = (MarginLayoutParams) mImageArrow
                    .getLayoutParams();
            lp.rightMargin = arrowRight;
            mImageArrow.setLayoutParams(lp);
        }

    }

    /**
     * 设置箭头状态
     * @param isShowArrow true:显示,false:隐藏
     */
    public void setArrowVisibility(boolean isShowArrow){
        mImageArrow.setVisibility(isShowArrow ? View.VISIBLE : View.GONE);
    }

    /**
     * 初始化指示点
     * @param ta
     */
    private void initPoint(TypedArray ta) {
        boolean isShowPoint = ta.getBoolean(
                R.styleable.AccordionTextView_accShowPoint, true);
        if (!isShowPoint) {
            mImagePoint.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 设置指示点状态
     * @param isShowPoint true:显示,false:隐藏
     */
    public void setPointVisibility( boolean isShowPoint){
        mImagePoint.setVisibility(isShowPoint ? View.VISIBLE : View.INVISIBLE);
    }

    public void setTextName(String key) {
        mTextName.setText(TextUtils.isEmpty(key) ? "" : key);
    }

    public void setTextValue(String value) {
        mTextValue.setText(TextUtils.isEmpty(value) ? "" : value);
    }

    public String getTextValue() {
        return mTextValue.getText().toString();
    }

    private int dp2px(float pxValue) {
        float reSize = getResources().getDisplayMetrics().density;
        return (int) ((pxValue / reSize) + 0.5);
    }

}
